package us.mooresr.webtroopmanager.client.activity;

import java.util.ArrayList;
import java.util.List;

import us.mooresr.webtroopmanager.client.ClientFactory;
import us.mooresr.webtroopmanager.client.TroopManagerService;
import us.mooresr.webtroopmanager.client.TroopManagerServiceAsync;
import us.mooresr.webtroopmanager.client.place.EventDriversPlace;
import us.mooresr.webtroopmanager.client.ui.EventDriversView;
import us.mooresr.webtroopmanager.shared.EventDriverSummary;
import us.mooresr.webtroopmanager.shared.PersonSummary;
import us.mooresr.webtroopmanager.shared.VehicleSummary;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class EventDriversActivity extends MyActivity implements EventDriversView.Presenter {

	
	public EventDriversActivity(EventDriversPlace place,
			ClientFactory clientFactory) {
		super(place, clientFactory);
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		super.start(panel, eventBus);
		
		EventDriversView view = clientFactory.getEventDriversView();
		view.setPresenter(this);
		loadData();
		panel.setWidget(view.asWidget());
		
	}

	private void loadData() {
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		
		AsyncCallback<List<EventDriverSummary>> callback = new AsyncCallback<List<EventDriverSummary>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(List<EventDriverSummary> result) {
				EventDriversView view = clientFactory.getEventDriversView();
				view.setInitialDrivers(result);
				loadPeople();
				
			}
			
		};
		
		Long eventId = ((EventDriversPlace)place).getEventId();
		troopManagerService.getEventDrivers(eventId, callback);
		
	}

	protected void loadPeople() {
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		
		AsyncCallback<List<PersonSummary>> callback = new AsyncCallback<List<PersonSummary>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(List<PersonSummary> result) {
				EventDriversView view = clientFactory.getEventDriversView();
				view.setInitialPeople(result);
				loadVehicles();
				
			}
			
		};
		
		troopManagerService.getAllAdults(callback);
		
	}

	protected void loadVehicles() {
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		
		AsyncCallback<List<VehicleSummary>> callback = new AsyncCallback<List<VehicleSummary>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(List<VehicleSummary> result) {
				EventDriversView view = clientFactory.getEventDriversView();
				view.setInitialVehicles(result);
				
			}
			
		};
		
		troopManagerService.getAllVehicles(callback);
		
	}

	@Override
	public void storeEventDrivers(ArrayList<EventDriverSummary> drivers) {
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		Long eventId = ((EventDriversPlace)place).getEventId();
		
		AsyncCallback<Long> callback = new AsyncCallback<Long>() {

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
				
			}

			@Override
			public void onSuccess(Long result) {
				// TODO Auto-generated method stub
				
			}
			
		};
		
		troopManagerService.setEventDrivers(eventId, drivers, callback);
		
	}

	@Override
	public Long getEventId() {
		return ((EventDriversPlace) place).getEventId();
	}

	@Override
	public void refreshLists() {
		loadPeople();
		
	}


	
	

}
